• Jump To … +
    main.js separate.js single.js web-apg-api.js main.js web-conv-api.js ast.js csv.js dangling-else.js display.js flags.js float.js limits.js main.js multiline-mode.js recursive.js replace.js rules.js split.js testonly.js trace.js udt.js unicode.js web-email.js word-boundaries.js main.js phone-number.js web-main.js web-phone-number.js main.js phone-number.js setup.js translate.js xml.js branch-fail-grammar.js main.js parent-mode-grammar.js setup.js universal-mode-grammar.js colors-app.js colors-callbacks.js colors.js main.js more-app.js more-setup.js more.js ast-callbacks.js bad-input.js basic.js ini-file.js main.js parser-callbacks.js setup.js trace.js anbncn.js and.js c-comment.js compound.js main.js nested.js not.js setup.js boundaries-grammar.js boundaries.js comment-grammar.js comment.js main.js negative-grammar.js negative.js positive-grammar.js positive.js setup.js main.js odata-grammar.js run.js setup.js area-code.js lookaround.js main.js phone-number.js setup.js simple.js all-operators.js default.js fancy-number.js limited-lines.js main.js select-operators.js select-rules.js setup.js main.js minimal.js parent-u.js parent.js phone-number.js setup.js stats.js trace.js universal-u.js universal.js callbacks.js grammar.js main.js parser.js writeHtml.js LICENSE.md README.md index.md
  • unicode.js

  • §
    /*  *************************************************************************************
     *   copyright: Copyright (c) 2021 Lowell D. Thomas, all rights reserved
     *     license: BSD-2-Clause (https://opensource.org/licenses/BSD-2-Clause)
     *   ********************************************************************************* */
  • §

    With the Unicode flag, u, set, apg-exp returns its matched phrases as arrays of character codes rather than Javascript strings. This module gives a short demonstration of how one might take advantage of that to deal with Unicode text. I don’t read or speak Greek, but I took a couple of phrases from here to experiment with.

    Here I will simply define a Greek word as any string of Unicode characters from the Greek and Coptic range U+0370-03FF. The exercise will be to find and display any Greeks word fitting this loose description.

    (function unicode() {
      try {
        const { apgExp: ApgExp, apgLib } = require('apg-js');
        const writeHtml = require('../writeHtml');
        const grammar = new (require('./grammars/greek-words'))();
  • §

    const apgJs = require(‘apg-js’); const { apgExp } = apgJs; const { apgLib } = apgJs;

        let result;
        let str;
        let html;
        const flags = 'ug';
        const exp = new ApgExp(grammar, flags);
        console.log();
        console.log('Demonstrate Unicode mode with the "u" flag.');
        console.log();
        console.log('SABNF grammar:');
        console.log(exp.sourceToText());
        str = '';
        str += 'The word for "yes" is \u039d\u03b1\u03b9\n';
        str += 'The word for "no" is \u038c\u03c7\u03b9\n';
        str += 'The word for "maybe" is \u038a\u03c3\u03c9\u03c2\n';
        console.log();
        console.log('input string:');
        console.log(str);
        html = '';
        html += '<h3>Greek words found</h3>\n';
        const TRUE = true;
        while (TRUE) {
          result = exp.exec(str);
          if (result == null) {
            break;
          }
          const greek = result.rules['greek-word'];
  • §

    When a Greek word is found, its character codes are converted to HTML entities for web page display.

          if (greek) {
            /* generate text output for the console */
            /* (displays Greek characters on my Ubuntu console) */
            const txt = apgLib.utils.charsToString(greek[0].phrase);
            console.log(`Greek word found: ${txt}`);
            /* convert the characters to HTML entities for page display */
            html += 'Greek word: ';
            for (let i = 0; i < greek[0].phrase.length; i += 1) {
              html += `&#${greek[0].phrase[i]};`;
            }
            html += '<br>\n';
          }
        }
        /* display the greek words, represented as HTML entities, on a page */
        const page = apgLib.utils.htmlToPage(html);
        const htmlName = 'unicode-words';
        writeHtml(page, htmlName);
      } catch (e) {
        console.log(`EXCEPTION: ${e.message}`);
      }
    })();